﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class IdNameController : Controller
    {
        public IIdNameService idNameSer { get; set; }
        // GET: IdName
        [CheckPermission("Dic.List")]
        public ActionResult List()
        {
            var model = idNameSer.GetAll();
            return View(model);
        }

        // 删除
        [CheckPermission("Dic.Delete")]
        public ActionResult Delete(long idNameId)
        {
            idNameSer.MakeDelete(idNameId);
            ViewBag.totalCount = idNameSer.GetAll().Count();
            return Json(new AjaxResult { Status = "ok" });
        }
        // 批量删除
        [CheckPermission("Dic.Delete")]
        public ActionResult BatchDel(long[] idNameIds)
        {
            foreach (var idNameId in idNameIds)
            {
                idNameSer.MakeDelete(idNameId);
            }
            return Json(new AjaxResult { Status = "ok" });
        }
        // 更新
        [CheckPermission("Dic.Edit")]
        [HttpGet]
        public ActionResult Edit(long idNameId)
        {
            var model = idNameSer.GetById(idNameId);
            return View(model);
        }
        [CheckPermission("Dic.Edit")]
        [HttpPost]
        public ActionResult Edit(long idNameId, string typeName, string name)
        {
            IdNameDTO dto = new IdNameDTO();
            dto.CreateDateTime = DateTime.Now;
            dto.Id = idNameId;
            dto.Name = name;
            dto.TypeName = typeName;
            idNameSer.Updata(dto);
            return Json(new AjaxResult { Status = "ok" });
        }

        // 添加
        [CheckPermission("Dic.Add")]
        [HttpGet]
        public ActionResult AddNew()
        {
            return View();
        }
        [CheckPermission("Dic.Add")]
        [HttpPost]
        public ActionResult AddNew(string typeName, string name)
        {
            IdNameDTO dto = new IdNameDTO();
            dto.CreateDateTime = DateTime.Now;
            dto.Name = name;
            dto.TypeName = typeName;
            long id = idNameSer.AddNew(dto);
            if (id <= 0)
            {
                return Json(new AjaxResult { Status = "error" });
            }
            return Json(new AjaxResult { Status = "ok" });
        }
    }
}